package com.raising.modules.cms.controller;

import com.raising.framework.controller.BaseController;
import com.raising.framework.entity.ResultVo;
import com.raising.framework.interceptor.Page;
import com.raising.modules.cms.service.ArticleService;
import com.raising.modules.cms.vo.Article;
import com.raising.modules.sys.utils.DictUtils;
import com.raising.utils.JStringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * 文章表 控制器
 * @author gaoy
 * @createTime
 */
@Controller
@RequestMapping("${adminPath}/cms/article")
public class ArticleController extends BaseController {

    @Autowired
    private ArticleService articleService;
    /**
     * 获取单个对象（共用）
     */
    @ModelAttribute
    public Article get(@RequestParam(required = false) String id) {
        Article entity = null;
        if (JStringUtils.isNotBlank(id)) {
            entity = articleService.get(id);
        }
        if (entity == null) {
            entity = new Article();
        }
        return entity;
    }
    /**
    * 跳转至列表页面
    * @author gaoy
    * @createTime
    */
    @RequiresPermissions("cms:article:view")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list() {
        return "cms/article/list";
    }

    /**
    * 加载列表分页数据
    * @author gaoy
    * @createTime
    */
    @RequiresPermissions("sys:role:view")
    @RequestMapping(value = "/getPage.json", method = RequestMethod.POST)
    public @ResponseBody
    ResultVo getPage(Model model, Article article, Page<Article> page) {
        page.setEntity(article);
        page.setOrderBy(" a.id asc ");
        return new ResultVo(ResultVo.SUCCESS,"ok",articleService.getPage(page));
    }

    /**
    * 跳转新增页面
    * @author gaoy
    * @createTime
    */
    @RequiresPermissions("cms:article:create")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create(Article article,Model model) {
        model.addAttribute("article", article);
        // 是否可用
        model.addAttribute("yn", DictUtils.getDictList("DIM_YN"));
        return "cms/article/add";
    }

    /**
    * 执行新增操作
    * @author gaoy
    * @createTime
    */
    @RequiresPermissions("cms:article:create")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public String create(Article article, RedirectAttributes redirectAttributes) {
        articleService.save(article);
        addMessage(redirectAttributes,"新增成功");
        return "redirect:"+ adminPath +"/cms/article/update?id="+article.getId();
    }
    /**
    * 跳转更新页面
    * @author gaoy
    * @createTime
    */
    @RequiresPermissions("cms:article:update")
    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public String update(Article article, Model model) {
        model.addAttribute("article", article);
        // 是否可用
        model.addAttribute("yn", DictUtils.getDictList("DIM_YN"));
        return "cms/article/edit";
    }
    /**
    * 执行更新操作
    * @author gaoy
    * @createTime
    */
    @RequiresPermissions("cms:article:update")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(Article article, RedirectAttributes redirectAttributes) {
        articleService.save(article);
        addMessage(redirectAttributes,"修改成功");
        return "redirect:"+ adminPath +"/cms/article/update?id="+article.getId();
    }
    /**
    * 执行删除操作
    * @author gaoy
    * @createTime
    */
    @RequiresPermissions("cms:article:delete")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String delete(String id,int pageNo,int pageSize, RedirectAttributes redirectAttributes) {
        articleService.delete(id);
        addMessage(redirectAttributes,"删除成功");
        return "redirect:"+ adminPath +"/cms/article/list?pageNo="+pageNo+"&pageSize="+pageSize;
    }
}
